package com.xjgc.eip.panel.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjgc.common.utils.DateUtil;
import com.xjgc.common.utils.PushDataUtil;
import com.xjgc.common.utils.TransNoUitl;
import com.xjgc.eip.panel.entity.ZxjEipScdd;
import com.xjgc.eip.panel.mapper.ZxjEipScddMapper;
import com.xjgc.eip.panel.service.ZxjEipScddService;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhanghongbin
 * @since 2019-11-17
 */
@Service
@DS("proxyDs")
public class ZxjEipScddServiceImpl extends ServiceImpl<ZxjEipScddMapper, ZxjEipScdd> implements ZxjEipScddService {
    @Value("${scddUrl}")
    public String url;
    @Autowired
    private ZxjEipScddMapper zxjEipScddMapper;
    public void productOrder() {
        QueryWrapper<ZxjEipScdd> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("ioflag","0");
        List<ZxjEipScdd> list = this.list(queryWrapper);
//        ReturnResult returnResult = new ReturnResult();
//        returnResult.setData(list);
        String orisys = "0";
        String transno = TransNoUitl.genTransNo();
        String operateType = "add";
        for (ZxjEipScdd zes : list) {
            //header数据
            if ("1".equals(zes.getStatus())) {
                operateType = "update";
            }
            //body数据  就是zex
            //String url = "http://10.10.7.70:22108/api/supplier-ipo";
            String returnString = PushDataUtil.pushData(url, orisys, transno, operateType, zes);
            if (returnString == null) {
                System.out.println("=======================》》》》》》》》》》无返回数据");
                return;
            }
            JSONObject jsonObject = JSONObject.parseObject(returnString);
            String status = jsonObject.getString("status");
            String message = jsonObject.getString("message");
            if("0".equals(status)||"00000".equals(status)||"10000".equals(status)){
                zes.setIoflag("0");
                zes.setMessage("入库成功");
                zes.setPushTime(new Date());
            }else{
                zes.setIoflag(status);
                zes.setMessage(message);
                zes.setPushTime(new Date());
            }
            this.updateById(zes);
        }
    }
    @Override
    public Map selectByTime(Map map) {
        return zxjEipScddMapper.selectByTime(map);
    }

    @Override
    public Map selectByAreaTime() throws ParseException {
        //统计map
        Map allMap=new HashMap();
        SimpleDateFormat format=new SimpleDateFormat("yyyyMMdd");
        Date nowDate=new Date();
        String nowDateString=format.format(nowDate);
        Integer nowDateDay=Integer.parseInt(nowDateString.substring(nowDateString.length()-2));
        Date startDate=null;
        if(nowDateDay>6){
            Integer nowWeakDay=Integer.parseInt(nowDateString);
            Integer firstWeakDay=(nowWeakDay-6);
            startDate=format.parse(firstWeakDay.toString());
        }else {
            startDate= DateUtil.getBeginDayOfMonth();
        }

        HashMap weakMap=new HashMap();
        weakMap.put("startTime",startDate);
        weakMap.put("endTime",nowDate);
        List<Map> oneWeakMap=zxjEipScddMapper.selectByAreaTime(weakMap);

        Map<String,List<Map>>dataMap=new HashMap<>();
        for (Map map:oneWeakMap){
            if(dataMap.get(map.get("pushTime"))!=null){
                dataMap.get(map.get("pushTime")).add(map);
            }else{
                List<Map> list=new ArrayList<>();
                list.add(map);
                dataMap.put(map.get("pushTime").toString(),list);
            }
        }
//        //周统计 第一周
//        Date fistDate=DateUtil.getBeginDayOfMonth();
//        String firstDayString=format.format(fistDate);
//        int firstDayInt=Integer.parseInt(firstDayString);
//        Integer firstWeak=firstDayInt+6;
//        Date firtWeakDate=format.parse(firstWeak.toString());
//        //山东电子
//        HashMap weakMap=new HashMap();
//        weakMap.put("startTime",fistDate);
//        weakMap.put("endTime",firtWeakDate);
//        List<Map> firstWeakReturnMap=zxjEipXsddMapper.selectByAreaTime(weakMap);
//        allMap.put("firstWeak",firstWeakReturnMap);
//        Integer secondWeak=0;
//        Integer secondWeakLast=0;
//        Integer threeWeakLast=0;
//        Integer fourWeakLast=0;
//        if (nowDateDay.intValue()>7) {
//            //第二周
//            secondWeak=firstWeak+1;
//            Date firstOfSecondWeakDate=format.parse(secondWeak.toString());
//            secondWeakLast=secondWeak+6;
//            Date lastOfSecondWeakDate=format.parse(secondWeakLast.toString());
//            weakMap.put("startTime",firstOfSecondWeakDate);
//            weakMap.put("endTime",lastOfSecondWeakDate);
//            List<Map> secondWeakReturnMap=zxjEipXsddMapper.selectByAreaTime(weakMap);
//            allMap.put("secondWeak",secondWeakReturnMap);
//        }
//        if (nowDateDay.intValue()>14) {
//            //第三周
//            Integer threeWeak=secondWeakLast+1;
//            Date firstOfThreeWeakDate=format.parse(threeWeak.toString());
//            threeWeakLast=threeWeak+6;
//            Date lastOfThreeWeakDate=format.parse(threeWeakLast.toString());
//            weakMap.put("startTime",firstOfThreeWeakDate);
//            weakMap.put("endTime",lastOfThreeWeakDate);
//            List<Map> threeWeakReturnMap=zxjEipXsddMapper.selectByAreaTime(weakMap);
//            allMap.put("threeWeak",threeWeakReturnMap);
//        }
//        if (nowDateDay.intValue()>20) {
//            //第四周
//            Integer fourWeak=threeWeakLast+1;
//            Date firstOfFourWeakDate=format.parse(fourWeak.toString());
//            fourWeakLast=fourWeak+6;
//            Date lastOfFourWeakDate=format.parse(fourWeakLast.toString());
//            weakMap.put("startTime",firstOfFourWeakDate);
//            weakMap.put("endTime",lastOfFourWeakDate);
//            List<Map> fourWeakReturnMap=zxjEipXsddMapper.selectByAreaTime(weakMap);
//            allMap.put("fourWeak",fourWeakReturnMap);
//        }
//        if(nowDateDay.intValue()>27){
//            //第五周
//            Integer fiveWeak=fourWeakLast+1;
//            Date firstOfFiveWeakDate=format.parse(fiveWeak.toString());
//            Date lastOfMonthDay=DateUtil.getEndDayOfMonth();
//            weakMap.put("startTime",firstOfFiveWeakDate);
//            weakMap.put("endTime",lastOfMonthDay);
//            List<Map> fiveWeakReturnMap=zxjEipXsddMapper.selectByAreaTime(weakMap);
//            allMap.put("fiveWeak",fiveWeakReturnMap);
//        }
        return dataMap;
    }

    @Override
    public Map selectByMonth() throws ParseException {
        //统计map
        Map allMap=new HashMap();
        SimpleDateFormat format=new SimpleDateFormat("yyyyMMdd");
        Date nowDate=new Date();
        String nowDateString=format.format(nowDate);
        Integer nowDateDay=Integer.parseInt(nowDateString.substring(nowDateString.length()-2));
        //周统计 第一周
        Date fistDate=DateUtil.getBeginDayOfMonth();
        String firstDayString=format.format(fistDate);
        int firstDayInt=Integer.parseInt(firstDayString);
        Integer firstWeak=firstDayInt+6;
        Date firtWeakDate=format.parse(firstWeak.toString());
        HashMap weakMap=new HashMap();
        weakMap.put("startTime",fistDate);
        weakMap.put("endTime",firtWeakDate);
        List<Map> firstWeakReturnMap=zxjEipScddMapper.selectByAreaTime(weakMap);
        allMap.put("firstWeak",firstWeakReturnMap);
        Integer secondWeak=0;
        Integer secondWeakLast=0;
        Integer threeWeakLast=0;
        Integer fourWeakLast=0;
        if (nowDateDay.intValue()>7) {
            //第二周
            secondWeak=firstWeak+1;
            Date firstOfSecondWeakDate=format.parse(secondWeak.toString());
            secondWeakLast=secondWeak+6;
            Date lastOfSecondWeakDate=format.parse(secondWeakLast.toString());
            weakMap.put("startTime",firstOfSecondWeakDate);
            weakMap.put("endTime",lastOfSecondWeakDate);
            List<Map> secondWeakReturnMap=zxjEipScddMapper.selectByMonth(weakMap);
            allMap.put("secondWeak",secondWeakReturnMap);
        }
        if (nowDateDay.intValue()>14) {
            //第三周
            Integer threeWeak=secondWeakLast+1;
            Date firstOfThreeWeakDate=format.parse(threeWeak.toString());
            threeWeakLast=threeWeak+6;
            Date lastOfThreeWeakDate=format.parse(threeWeakLast.toString());
            weakMap.put("startTime",firstOfThreeWeakDate);
            weakMap.put("endTime",lastOfThreeWeakDate);
            List<Map> threeWeakReturnMap=zxjEipScddMapper.selectByMonth(weakMap);
            allMap.put("threeWeak",threeWeakReturnMap);
        }
        if (nowDateDay.intValue()>20) {
            //第四周
            Integer fourWeak=threeWeakLast+1;
            Date firstOfFourWeakDate=format.parse(fourWeak.toString());
            fourWeakLast=fourWeak+6;
            Date lastOfFourWeakDate=format.parse(fourWeakLast.toString());
            weakMap.put("startTime",firstOfFourWeakDate);
            weakMap.put("endTime",lastOfFourWeakDate);
            List<Map> fourWeakReturnMap=zxjEipScddMapper.selectByMonth(weakMap);
            allMap.put("fourWeak",fourWeakReturnMap);
        }
        if(nowDateDay.intValue()>27){
            //第五周
            Integer fiveWeak=fourWeakLast+1;
            Date firstOfFiveWeakDate=format.parse(fiveWeak.toString());
            Date lastOfMonthDay=DateUtil.getEndDayOfMonth();
            weakMap.put("startTime",firstOfFiveWeakDate);
            weakMap.put("endTime",lastOfMonthDay);
            List<Map> fiveWeakReturnMap=zxjEipScddMapper.selectByMonth(weakMap);
            allMap.put("fiveWeak",fiveWeakReturnMap);
        }
        return allMap;
    }

    @Override
    public List<Map> selectByJiDu() {
        Integer year=DateUtil.getNowYear();
        Integer endMonth=DateUtil.getNowMonth();
        Map weakMap=new HashMap();
        weakMap.put("startTime",year+"-"+(endMonth-2));
        weakMap.put("endTime",endMonth);
        List<Map> fiveWeakReturnMap=zxjEipScddMapper.selectByJiDu(weakMap);
        return fiveWeakReturnMap;
    }

    @Override
    public Map selectByYear() {
        Integer year=DateUtil.getNowYear();
        Integer endMonth=DateUtil.getNowMonth();
        Map<String,List<Map>> map=new HashMap<>();
        if(endMonth<3){
            Map weakMap=new HashMap();
            weakMap.put("startTime",year+"-"+"01");
            weakMap.put("endTime",endMonth);
            List<Map> jiDuMap=zxjEipScddMapper.selectByYear(weakMap);
            map.put("第一季度",jiDuMap);
        }else{
            if(endMonth>3&& endMonth<=6){
                Map weakMap=new HashMap();
                weakMap.put("startTime",year+"-"+"04");
                weakMap.put("endTime",endMonth);
                List<Map> jiDuMap=zxjEipScddMapper.selectByYear(weakMap);
                map.put("第二季度",jiDuMap);
            }
            if(endMonth>6&& endMonth<=9){
                Map weakMap=new HashMap();
                weakMap.put("startTime",year+"-"+"07");
                weakMap.put("endTime",endMonth);
                List<Map> jiDuMap=zxjEipScddMapper.selectByYear(weakMap);
                map.put("第三季度",jiDuMap);
            }
            if(endMonth>9&& endMonth<=12){
                Map weakMap=new HashMap();
                weakMap.put("startTime",year+"-"+"10");
                weakMap.put("endTime",endMonth);
                List<Map> jiDuMap=zxjEipScddMapper.selectByYear(weakMap);
                map.put("第四季度",jiDuMap);
            }
        }
        return map;
    }
}
